package com.central.apps.handler.contract;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.central.app.service.HandlerService;
import com.central.apps.common.util.DateUtiles;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2022/9/27.
 */
@Slf4j
@Component
public class ConfinalHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private ConchangeHandler conchangeHandler;
    @Autowired
    private FileSynUtilService fileSynUtilService;
    @Autowired
    private ConreviewHandler conreviewHandler;

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {


    }

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {

        if (flowNode.getId().equals("approvaltwo")) {
            //合同关闭前，判断付款节点是否全部完成
            boolean flag = op.isFlag();//判断是否审核通过
            if (flag) {
                //查询接收入库主表gq_concollandpay
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_confinal where id=" + id);
                if (CollectionUtils.isNotEmpty(list)) {
                    String sql = "select * from gq_connodenew where concode = '" + list.get(0).get("concode") + "' and `status` !='已完成'";
                    List<Map<String, Object>> list2 = handlerService.findBySql(sql);
                    if (CollectionUtils.isNotEmpty(list2)) {
                        return "请完成剩余付款节点，再关闭合同";
                    }
                }

            }
        }

        //填充会签人员
        if ("submit".equals(flowNode.getId())) {
            List<Map<String, Object>> list3 = handlerService.findBySql("select isonline,concode,conid from gq_confinal where id = " + id);
            String concode = (String) list3.get(0).get("concode");
            String conid = (String) list3.get(0).get("conid");
            if ("是".equals(list3.get(0).get("isonline"))) {
                //如果是线上评审，需要增加评审人员
                boolean res = conreviewHandler.isSetPersons(app, id);
                if (!res) {
                    return "选择线上评审后，需要添加评审组成员信息再提交审批";
                }
                conreviewHandler.setcountersigns(app, id,app);
            }
            //需要提交交付物清单再提交
//            List<Map<String,Object>> list = handlerService.findBySql("select id from gq_deliverylist where concode = '"+concode+"'");
//            if(CollectionUtils.isEmpty(list)){
//                return "请新增交付物清单检查再提交";
//            }
            List<Map<String,Object>> list = handlerService.findBySql("select id from gq_filelist where conid = '"+conid+"' and status='未上传'");
            if(CollectionUtils.isNotEmpty(list)){
                return "请上传所有的交付物文件再提交";
            }
        }
        //会签审批通过后，把会签意见同步到意见历史中
        if ("countersign".equals(flowNode.getId())) {
            boolean res = conreviewHandler.isOpinion(app, id);
            if (!res) {
                return "请填写审批意见再提交";
            }
            //conreviewHandler.setRevcoments(app, id);
            //提交会见意见后，置空主表评审意见
            conreviewHandler.setnullrevcomments(id,"gq_confinal");
        }
        //-----------------审批后代码逻辑-------------------
        //合同验收
        if (flowNode.getId().equals("approval")) {
            //审核通过后增加库存
            boolean flag = op.isFlag();//判断是否审核通过
            if (flag) {
                //查询接收入库主表gq_concollandpay
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_confinal where id=" + id);
                if (CollectionUtils.isNotEmpty(list)) {

                    try {
                        String ispublicity = (String) list.get(0).get("ispublicity");//是否公示
                        if ("是".equals(ispublicity)) {
                            //conchangeHandler.updateStatus((String) list.get(0).get("concode"), "公示中");
                            //conchangeHandler.updateStatusByid(id,"公示中");
                            conchangeHandler.updateStatus((String) list.get(0).get("concode"), "公示中");
                            //设置公示结束时间 ,公示期中的签订页面不许修改，只能查看
                            int publicity = (int)list.get(0).get("publicity");
                            Date publicityend = com.central.apps.handler.until.DateUtiles.stepDay(new Date(),publicity);
                            Map<String,Object> map = new HashMap<>();
                            map.put("id",id);
                            map.put("publicityend",publicityend);
                            handlerService.update("gq_confinal",map);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String concode = (String) list.get(0).get("concode");
//                    //将上传的文件，添加到合同台账里边
//                    String concode = (String)list.get(0).get("concode");
//                    fileSynUtilService.insertFiles(app,id,concode);
//                    String sql = "update gq_conaccount set constatus ='验收完成'  where concode = '" + concode + "'";
//                    handlerService.updateBySql(sql);
//                    String sql2 = "update gq_coninfo set constatus ='验收完成'  where concode = '" + concode + "'";
//                    handlerService.updateBySql(sql2);
                    try {
                        conchangeHandler.updateStatus(concode, "验收完成");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //流程归档
                    fileSynUtilService.processarc(app, id + "", "合同验收", (String) list.get(0).get("concode"), (String) list.get(0).get("conname"));
                }
            }
        }

        //合同关闭
//        if (flowNode.getId().equals("approvaltwo")) {
//            //审核通过后增加库存
//            boolean flag = op.isFlag();//判断是否审核通过
//            if (flag) {
//                //查询接收入库主表gq_concollandpay
//                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_confinal where id=" + id);
//                if (CollectionUtils.isNotEmpty(list)) {
//                    //将上传的文件，添加到合同台账里边
//                    String concode = (String) list.get(0).get("concode");
//                    fileSynUtilService.insertFiles(app, id, concode);
//                    //将合同状态更改为关闭
//                    String closttime = DateUtiles.getYmdhms(new Date());
//                    String sql = "update gq_conaccount set constatus ='合同关闭' , closttime = '" + closttime + "' where concode = '" + concode + "'";
//                    //String sql2 = "update gq_coninfo set constatus ='合同关闭'  where concode = '" + concode + "'";
//                    handlerService.updateBySql(sql);
//                    try {
//                        conchangeHandler.updateStatus(concode, "合同关闭");
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    //handlerService.updateBySql(sql2);
//                }
//            }
//        }
        return super.beforeExcuteFlow(app, id, flowNode, op);
    }

    @Override
    public void afterMoveNode(String app, Long id) throws Exception {
        //驳回后同步评审意见到历史评审意见
        conreviewHandler.setAllRevcoments(app,id);
    }
    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        //将主表中的评审意见添加到对应的子表评审意见位置。
        conreviewHandler.moverevcomments(data);
    }
}
